package com.zhiyou.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zhiyou.model.FyCourse;
import com.zhiyou.service.CourseService;
import com.zhiyou.utils.JsonResult;

@RestController
@RequestMapping("Course/")
public class CourseController {

	@Autowired
	CourseService courseService;

	/**
	 * 添加图书科目信息
	 * 
	 * @param course
	 * @param request
	 * @param response
	 * @return
	 */
	@PostMapping("add")
	public JsonResult add(FyCourse course, HttpServletRequest request, HttpServletResponse response) {
		try {
			courseService.add(course);
			return new JsonResult("插入科目成功");
		} catch (Exception e) {
			return new JsonResult(500, "插入失败");
		}
	}

	/**
	 * 更新图书科目信息
	 * 
	 * @param course
	 * @param request
	 * @param response
	 * @return
	 */
	@PutMapping("update")
	public JsonResult update(FyCourse course, HttpServletRequest request, HttpServletResponse response) {
		try {
			courseService.update(course);
			;
			return new JsonResult("修改科目成功");
		} catch (Exception e) {
			return new JsonResult(500, "修改失败");
		}
	}

	/**
	 * 根据科目id删除科目
	 * 
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@DeleteMapping("delete/{id}")
	public JsonResult delete(@PathVariable("id") int id, HttpServletRequest request, HttpServletResponse response) {
		try {
			courseService.delete(id);
			return new JsonResult("删除科目成功");
		} catch (Exception e) {
			return new JsonResult(500, "删除失败");
		}
	}

	/**
	 * 根据科目id批量删除科目
	 * 
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@PostMapping("delete")
	public JsonResult deleteAll(int[] ids, HttpServletRequest request, HttpServletResponse response) {
		try {
			System.out.println(ids);
			courseService.deleteAll(ids);
			return new JsonResult("删除科目成功");
		} catch (Exception e) {
			return new JsonResult(500, "删除失败");
		}
	}

	/**
	 * 根据科目id查询科目详细信息
	 * 
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@GetMapping("{id}")
	public JsonResult selectById(@PathVariable("id") int id, HttpServletRequest request, HttpServletResponse response) {
		try {
			FyCourse selectById = courseService.selectById(id);
			return new JsonResult(selectById, "查询科目详细信息成功");
		} catch (Exception e) {
			return new JsonResult(500, "查询失败");
		}
	}

	/**
	 * 查询全部科目信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@GetMapping("list")
	public JsonResult list(HttpServletRequest request, HttpServletResponse response) {
		try {
			List<FyCourse> list = courseService.selectAll();
			return new JsonResult(list, "查询科目成功");
		} catch (Exception e) {
			return new JsonResult(500, "查询失败");
		}
	}

}
